package com.ad.system;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.security.MessageDigest;
import java.util.Calendar;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

public class Common {

	public static String splitMatch(String from, String to, int matchOrder,
			String input) {
		if (input == null || input.length() == 0 || from == null
				|| from.length() == 0 || to == null || to.length() == 0)
			return null;

		String[] splits1 = input.split(from);
		String[] splits2 = null;
		if (splits1 == null || splits1.length <= 1) {
			return null;
		}
		int matches = 0;
		String result = "";
		for (int i = 1; i < splits1.length; i++) {
			splits2 = splits1[i].split(to);
			if (splits2 != null && !splits2[0].equals(splits1[i])) {
				result = splits2[0];
				matches++;
				if (matches == matchOrder) {
					break;
				}
			}
		}
		return result.trim();
	}

	public static String splitMatchFirst(String from, String to, String input) {
		return splitMatch(from, to, 1, input);
	}

	public static String splitMatchLast(String from, String to, String input) {
		return splitMatch(from, to, input.length(), input);
	}

	public static boolean isEmpty(String in) {
		return in == null || in.equals("");
	}

	@SuppressWarnings("unchecked")
	public static boolean isEmpty(List list) {
		return list == null || list.size() == 0;
	}

	public static String trimHTLMSpace(String in) {
		if (isEmpty(in)) {
			return null;
		}
		if (in.contains("&nbsp;")) {
			in = in.replace("&nbsp;", "");
		}
		return in;
	}

	public static String chineseDecode(String code) throws IOException {
		String result = "";
		if (!isEmpty(code)) {
			result = new String(URLDecoder.decode(code, "UTF-8"));
		}
		return result;
	}

	public static String chineseEncode(String str)
			throws UnsupportedEncodingException {
		String result = "";
		if (!isEmpty(str)) {
			result = URLEncoder.encode(str, "UTF-8");
			// result = bASE64Encoder.encode(str.getBytes());
		}
		return result;
	}

	public final static String MD5(String s) {
		char hexDigits[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
				'a', 'b', 'c', 'd', 'e', 'f' };
		try {
			byte[] strTemp = s.getBytes();
			MessageDigest mdTemp = MessageDigest.getInstance("MD5");
			mdTemp.update(strTemp);
			byte[] md = mdTemp.digest();
			int j = md.length;
			char str[] = new char[j * 2];
			int k = 0;
			for (int i = 0; i < j; i++) {
				byte byte0 = md[i];
				str[k++] = hexDigits[byte0 >>> 4 & 0xf];
				str[k++] = hexDigits[byte0 & 0xf];
			}
			return new String(str);
		} catch (Exception e) {
			return null;
		}
	}

	@SuppressWarnings("unchecked")
	public static Object popList(List list) {
		if (isEmpty(list)) {
			return null;
		} else {
			return list.get(0);
		}
	}

	public static String getIpAddr(HttpServletRequest request) {
		String ip = request.getHeader("x-forwarded-for");
		if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
			ip = request.getHeader("Proxy-Client-IP");
		}
		if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
			ip = request.getHeader("WL-Proxy-Client-IP");
		}
		if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
			ip = request.getRemoteAddr();
		}
		return ip;
	}

	public static int generateRamdon(int from, int to) {
		double r = Math.random();
		return (int) (r * (to - from) + from);
	}

	public static boolean isChieseCharacter(char c) {
		Character.UnicodeBlock ub = Character.UnicodeBlock.of(c);
		return ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS
				|| ub == Character.UnicodeBlock.CJK_COMPATIBILITY_IDEOGRAPHS
				|| ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS_EXTENSION_A;

	}

	public static boolean isChiese(char c) {
		return isChieseCharacter(c) || isChieseSymBols(c);
	}

	public static boolean isChieseSymBols(char c) {
		Character.UnicodeBlock ub = Character.UnicodeBlock.of(c);
		return ub == Character.UnicodeBlock.GENERAL_PUNCTUATION
				|| ub == Character.UnicodeBlock.CJK_SYMBOLS_AND_PUNCTUATION
				|| ub == Character.UnicodeBlock.HALFWIDTH_AND_FULLWIDTH_FORMS;
	}

	public static boolean isLetter(char c) {
		return c >= 'a' && c <= 'z' || c >= 'A' && c <= 'Z';
	}

	public static String getDateString(int i) {
		Calendar calendar = Calendar.getInstance();
		calendar.add(Calendar.DATE, i);
		int month = calendar.get(Calendar.MONTH) + 1;
		int day = calendar.get(Calendar.DATE);
		String date = month + "月" + day + "日";
		return date;
	}

	public static void main(String[] args) {
		for (int i = 1; i > -35; i--) {
			System.out.println(i + ":" + getDateString(i));
		}
	}
}